using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DrinksSelector : MonoBehaviour
{
	private string drinksFile = "DataFiles/drinks";

	public List<string> drinksSections = new List<string>();

	public Dropdown dropDown;

	private GlassFillingUpController glassFillingUpController;

	private GameObject currentGlass;

	private void Start()
	{
		INIParser iNIParser = new INIParser();
		TextAsset name = Resources.Load<TextAsset>(drinksFile);
		iNIParser.Open(name);
		List<string> list = drinksSections = iNIParser.GetSections();
		iNIParser.Close();
		dropDown.ClearOptions();
		dropDown.AddOptions(drinksSections);
	}

	public void CreateDrink(string currentDrink)
	{
		if (currentGlass != null)
		{
			UnityEngine.Object.Destroy(currentGlass);
			currentGlass = null;
		}
		INIParser iNIParser = new INIParser();
		TextAsset name = Resources.Load<TextAsset>(drinksFile);
		iNIParser.Open(name);
		GameObject glassInstance = DrinksLoader.GetGlassInstance(iNIParser.ReadValue(currentDrink, "glass_id", string.Empty));
		glassInstance.transform.position = Vector3.down * 5f;
		glassInstance.name = currentDrink;
		GlassAddons component = glassInstance.GetComponent<GlassAddons>();
		if (component != null)
		{
			component.SetGlassBorderAddon(iNIParser.ReadValue(currentDrink, "addition_id", 0));
			component.SetStrawAddon(iNIParser.ReadValue(currentDrink, "straw_id", 0));
		}
		DrinkTargets component2 = glassInstance.GetComponent<DrinkTargets>();
		for (int i = 1; iNIParser.IsKeyExists(currentDrink, "liquid_" + i); i++)
		{
			LiquidType liquidType = LiquidTypes.GetLiquidType(iNIParser.ReadValue(currentDrink, "liquid_" + i, string.Empty));
			float item = (float)iNIParser.ReadValue(currentDrink, "liquid_" + i + "_percentage", 0.0);
			UnityEngine.Debug.Log(liquidType);
			component2.targetLiquidTypes.Add(liquidType);
			component2.targetLiquidHeights.Add(item);
		}
		Object.FindObjectOfType<LiquidPicker>().SetUpConnectionsWithGlassObject(glassInstance);
		glassFillingUpController = glassInstance.GetComponent<GlassFillingUpController>();
		StartCoroutine(ShowTargets(component2));
		iNIParser.Close();
		currentGlass = glassInstance;
	}

	private IEnumerator ShowTargets(DrinkTargets drinkTargets)
	{
		yield return new WaitForEndOfFrame();
		drinkTargets.SetTargets();
		List<TargetLine> targets = drinkTargets.GetTargetLinesList();
		foreach (TargetLine item in targets)
		{
			try
			{
				Object.FindObjectOfType<LiquidPicker>().LiquidPicked(LiquidTypes.GetLiquidName(item.liquidType));
				GlassFillingUpController obj = glassFillingUpController;
				Vector3 position = item.transform.position;
				obj.FillUpGlassPerfectly(position.y);
				glassFillingUpController.FillUpGlass(0f);
			}
			catch
			{
			}
		}
	}

	public void OnDropDownChanged(int value)
	{
		string currentDrink = drinksSections[value];
		CreateDrink(currentDrink);
	}
}
